package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.anno.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;

	@GetMapping(value = "/getUserInfoVo/{userId}")
	public Result<UserInfo> findByUserId(@PathVariable(value = "userId") Long userId) {
		UserInfo userInfo = userInfoService.getById(userId);
		return Result.ok(userInfo) ;
	}

	@GuiGuLogin(required = true)
	@Operation(summary = "判断用户是否购买声音列表")
	@PostMapping("userIsPaidTrack/{albumId}")
	public Result<Map<Long, Long>> userIsPaidTrack(@PathVariable Long albumId, @RequestBody List<Long> trackIdList) {
		Long userId = AuthContextHolder.getUserId();  // 通过feign的拦截器(RequestInterceptor)将用户id进行了透传
		Map<Long, Long> map = userInfoService.userIsPaidTrack(userId, albumId, trackIdList);
		return Result.ok(map);
	}

	@GetMapping(value = "/isPaidAlbum/{albumId}/{userId}")
	public Result<Boolean> isPaidAlbum(@PathVariable(value = "albumId") Long albumId , @PathVariable(value = "userId") Long userId) {
		Boolean isPaid = userInfoService.isPaidAlbum(albumId , userId) ;
		return Result.build(isPaid , ResultCodeEnum.SUCCESS) ;
	}

	@GuiGuLogin
	@GetMapping(value = "/findUserPaidTrackList/{albumId}")
	public Result<List<Long>> findUserPaidTrackList(@PathVariable(value = "albumId") Long albumId) {
		List<Long> trackIds = userInfoService.findUserPaidTrackList(albumId) ;
		return Result.build(trackIds , ResultCodeEnum.SUCCESS) ;
	}
}

